package library.and.xps.com.step.view.fragment;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import library.and.xps.com.step.R;
import library.and.xps.com.step.Constants;
import library.and.xps.com.step.util.RequestLocation;
import okhttp3.Call;

public class WeatherFragment extends Fragment {
    TextView tv_temp, tv_tianqi;
    ImageView iv_tianqi_img;
    LinearLayout weather_parent;
    ObjectAnimator objectAnimator;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_weather, container, false);
        tv_temp = (TextView) view.findViewById(R.id.tv_temp);
        tv_tianqi = (TextView) view.findViewById(R.id.tv_tianqi);
        iv_tianqi_img = (ImageView) view.findViewById(R.id.iv_tianqi_img);
        weather_parent = (LinearLayout) view.findViewById(R.id.weather_parent);
        objectAnimator = ObjectAnimator.ofFloat(weather_parent, "alpha", 0f, 1.0f);
        objectAnimator.setDuration(1000);
        init();
        return view;
    }

    void init() {
        RequestLocation requestLocation = RequestLocation.getInstance();
        requestLocation.setRequestLocal(new RequestLocation.onLocalResponse() {
            @Override
            public void onSuccess(BDLocation bdLocation) throws JSONException {
                if (bdLocation != null && !TextUtils.isEmpty(bdLocation.getCity())) {
                    requestWeatherInfo(bdLocation.getCity());
                }
            }

            @Override
            public void onFaild(String faild) {

            }
        });
        requestLocation.getLocation();
    }

    void requestWeatherInfo(String cityName) {
//        OkHttpUtils
//                .get()
//                .url(Constants.WEATHER + cityName)
//                .build()
//                .execute(new StringCallback() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//
//                    }
//
//                    @Override
//                    public void onResponse(String response, int id) {
//                        try {
//                            JSONObject jsonObject = new JSONObject(response);
//                            JSONObject data = jsonObject.getJSONObject("data");
//                            JSONArray array = data.getJSONArray("forecast");
//                            JSONObject jb = (JSONObject) array.get(0);
//                            analyzing(jb);
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });
    }

    void analyzing(JSONObject jb) {
        String high = "";
        String type = "";
        try {
            high = jb.getString("high");
            type = jb.getString("type");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (type.contains("云")) {
            iv_tianqi_img.setImageResource(R.mipmap.cloud);
        }else if(type.contains("晴")){
            iv_tianqi_img.setImageResource(R.mipmap.sunny);
        }else if(type.contains("阴")){
            iv_tianqi_img.setImageResource(R.mipmap.yin);
        }else if(type.contains("雨")){
            iv_tianqi_img.setImageResource(R.mipmap.rain);
        }
        tv_temp.setText(high);
        tv_tianqi.setText(type);
        objectAnimator.start();
    }

}
